##########################################################################################

library(data.table)
library(optparse)
library(dplyr)
library(parallel)

##########################################################################################

option_list <- list(
    make_option(c("--ccf_file"), type = "character") ,
    make_option(c("--sample_info"), type = "character") ,
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    ccf_file <- "~/20220915_gastric_multiple/dna_combinePublic/mutationTime/result/All_CCF_mutTime.tsv"
    sample_info <- "~/20220915_gastric_multiple/dna_combinePublic/config/tumor_normal.class.list"
    out_path <- "~/20220915_gastric_multiple/dna_combinePublic/mutationTime/result"

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

sample_info <- opt$sample_info
out_path <- opt$out_path
ccf_file <- opt$ccf_file

###########################################################################################

dir.create(out_path , recursive = T)

###########################################################################################

dat_info <- data.frame(fread(sample_info))
dat_ccf <- data.frame(fread(ccf_file))

###########################################################################################
maf_use <- dat_ccf

maf_use$vid <- paste(maf_use$Hugo_Symbol , 
    maf_use$Chromosome , maf_use$Start_Position , 
    maf_use$Reference_Allele , maf_use$Tumor_Seq_Allele2 , sep = ":")

maf_use <- merge( maf_use , dat_info[,c("Tumor" , "ID" , "Class")] , by.x = "Sample" , by.y = "Tumor" )

## 标记突变是共享还是私有
result <- c()
for( normal in unique(maf_use$ID) ){
    print(normal)
    tmp <- subset( maf_use , ID == normal )
    tmp2 <- Reduce(function(x,y)rbind(x,y),mclapply(1:length(unique(tmp$vid)),function(i){
        mut <- unique(tmp$vid)[i]
        tmp2 <- subset( tmp , vid == mut )
        share <- length(grep( "IM" , unique(tmp2$Class))) > 0 & length(grep( "GC" , unique(tmp2$Class))) > 0
        if(share){
            tmp2$Share <- TRUE
        }else{
            tmp2$Share <- FALSE
        }
        tmp2
    },mc.cores=10)) 

    result <- rbind( result , tmp2 )
}

###########################################################################################
## Mutations with f = 1 are denoted 
## as ‘clonal’, and mutations with f < 1 as ‘subclonal’. Mutations with f = 1 
## and m > 1 are denoted as ‘early clonal’ (co-amplified). In cases with f = 1, 
## m = 1 and C > 2, mutations were annotated as ‘late clonal’, if the minor 
## copy number was 0, otherwise ‘clonal’ (unspecified).

## WES
## 重新标记时间
result$CLS <- ifelse( result$CCF_adj < 0.6 , "subclonal" , result$CLS )
result$CLS <- ifelse( result$CCF_adj >= 0.6 , "clonal [NA]" , result$CLS )
result$CLS <- ifelse( result$CCF_adj >= 0.6 & result$MutCN == 1 & result$total_cn > 2 , "clonal [late]" , result$CLS )
result$CLS <- ifelse( result$CCF_adj >= 0.6 & result$MutCN == 1 & result$total_cn == 2 & result$minor_cn==0 , "clonal [late]" , result$CLS )
result$CLS <- ifelse( result$CCF_adj >= 0.6 & result$MutCN > 1 , "clonal [early]" , result$CLS )

###########################################################################################

dat_result <- result
dat_result$CLS <- ifelse( dat_result$Share == TRUE & dat_result$CCF_adj >= 0.6 , "clonal [share]" , dat_result$CLS )
dat_result$CLS <- ifelse( dat_result$Share == TRUE & dat_result$CCF_adj < 0.6 , "subclonal [share]" , dat_result$CLS )

out_name <- paste0(out_path , "/All_CCF_mutTime.addShare.tsv")
write.table( dat_result , out_name , row.names = F , sep = "\t" , quote = F )